'use strict';
const dbHelper = require('db-helper')
const uniID = require('uni-id')
const {
	uniIDConfigMerchant
} = require('config')

exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event: ', event)
	let {
		queryStringParameters
	} = event
	if (!queryStringParameters) {
		return {
			code: 500,
			msg: '参数不正确'
		}
	}
	let {
		id,
		token
	} = queryStringParameters

	if (!token) {
		return {
			code: 500,
			msg: 'token不存在'
		}
	}
	context.APPID = '__UNI__B9807A9'
	context.PLATFORM = 'h5'
	context.LOCALE = 'zh-Hans'
	const uniIDIns = uniID.createInstance({
		context: context,
		config: uniIDConfigMerchant
	})
	let payload = await uniIDIns.checkToken(token)
	if (payload.code) {
		return {
			code: 500,
			msg: 'token已过期'
		}
	}
	if (!id) {
		return {
			code: 500,
			msg: 'id不存在'
		}
	}

	try {
		let goods = await dbHelper.getById("t3ds-mall-goods", id)
		if (!goods) {
			return {
				code: 500,
				msg: "商品不存在"
			}
		}
		return {
			code: 200,
			data: goods.detail || ''
		}
	} catch (e) {
		console.error("获取商品详情失败", e);
		return {
			code: 500,
			msg: e.message
		}
	}
};